﻿using UnityEngine;
using System.Collections;
//using XInputDotNetPure; // Required in C#

public class DebugStuff : MonoBehaviour {

	public GameObject player;
	PlayerHealth plr_hp;
	
	public GameObject generator;
	GridGenerator ggen;

	float nearzoom = 10;
	float farzoom = 30;
	float start_z = 0f;
	
	// fps
	float updateInterval = 0.5f;
	float accum = 0f;
	int frames = 0;
	float timeLeft;
	string fps_str="FPS: 00.000000";
	

	// Use this for initialization
	void Start () {
	
		plr_hp = player.GetComponent<PlayerHealth>();
		ggen = generator.GetComponent<GridGenerator>();
		
		nearzoom=Camera.main.orthographicSize;
		farzoom = nearzoom*4;
		start_z = Camera.main.transform.position.z;
		
		timeLeft=updateInterval;
	}
	
	// Update is called once per frame
	void OnGUI () {
		
		float step = 16f;
		float y = 32f;
		
		if(ggen)
		{
			GUI.Label(new Rect(step,y,300f,y+20f),"Level parameters");y+=step;
			GUI.Label(new Rect(step*2,y,300f,y+20f),"Seed:         " + ggen.seed);y+=step;
			GUI.Label(new Rect(step*2,y,300f,y+20f),"Difficulty:   " + ggen.level_difficulty);y+=step;
			GUI.Label(new Rect(step*2,y,300f,y+20f),"Size:         " + ggen.GetSteps());y+=step;
		}
		else
		{
			GUI.Label(new Rect(step,y,300f,y+20f),"Pre-generated level");y+=step;
		}
		
		y+=step;
		GUI.Label(new Rect(step*2,y,300f,y+20f),"Collectables: "+GameObject.FindGameObjectsWithTag("Collectable").Length);y+=step;
		GUI.Label(new Rect(step*2,y,300f,y+20f),"Enemies: "+GameObject.FindGameObjectsWithTag("Enemy").Length);y+=step;
		GUI.Label(new Rect(step*2,y,300f,y+20f),"Boxes: "+GameObject.FindGameObjectsWithTag("Crate").Length);y+=step;
		
		y+=step;
		GUI.Label(new Rect(step*2,y,300f,y+20f),"Gun level: "+PlayerShooting.gun_level);y+=step;
		GUI.Label(new Rect(step*2,y,300f,y+20f),"HP: "+PlayerHealth.hp+" / "+PlayerHealth.max_hp);y+=step;
		GUI.Label(new Rect(step*2,y,300f,y+20f),"Charge: "+PlayerHealth.charge+" / "+PlayerHealth.max_charge);y+=step;
		
		y+=step;
		GUI.Label(new Rect(step*2,y,300f,y+20f),fps_str);y+=step;
	}
	void Update()
	{
		// fps
		timeLeft -= Time.deltaTime;
		accum += Time.timeScale/Time.deltaTime;
		++frames;
		
		if(timeLeft <= 0f)
		{
			float fps = accum / frames;
			fps_str = System.String.Format("FPS: {0:F2}",fps);
			
			timeLeft = updateInterval;
			accum = 0f;
			frames = 0;
		}
		
		// gun levelup
		if (Input.GetKeyDown(KeyCode.G))
			player.GetComponentInChildren<PlayerShooting>().AddGunLevel();
			
		// Restart
		if (Input.GetKey(KeyCode.R))
			Application.LoadLevel(Application.loadedLevel);
			
		// zoom out
		if(player.GetComponent<PlayerMovement>().zoom_btn)// || state.Buttons.LeftShoulder == ButtonState.Pressed || state.Buttons.RightShoulder == ButtonState.Pressed)
		{
			if(Camera.main.isOrthoGraphic)
			{
				Camera.main.orthographicSize=farzoom;
				float z = Camera.main.transform.position.z;
				Camera.main.transform.position = (ggen.level_start.transform.position +
				                                  ggen.level_finish.transform.position)/2f;
				                                  
				Camera.main.transform.position=new Vector3(Camera.main.transform.position.x,
				                                  			Camera.main.transform.position.y,z);
			}
			else
				transform.position = new Vector3(transform.position.x,transform.position.y,start_z*4);
		}
		else
		{
			if(Camera.main.isOrthoGraphic)
				Camera.main.orthographicSize=nearzoom;
			else
				transform.position = new Vector3(transform.position.x,transform.position.y,start_z);
		}
	}
}
